package com.my.main.common.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * JSON 共通应用类
 * @author 	王子莅
 * @editor	韩晗		直接输出Json字符串
 * @editor	韩晗	2015-01-28
 * 			增加将Json字符串转成List<Map>或Map对象的方法
 */
public class JSONUtil {
	
	
	public static final String ContentType_json = "application/json";
	
	
	/**
	 * 直接在action相应response对象中输出JSON字符串
	 * 输出后需在对应action返回"none", action设置可参照struts-showcase.xml中名为tree的action, result处值为空
	 * @param obj 传入任意类型对象
	 */
	public static void responseJSON(Object obj, SerializerFeature[] features) {
		HttpServletResponse res = ContextHolderUtils.getResponse();

		res.setContentType("text/html;charset=utf-8");
		res.setCharacterEncoding("utf-8");

		try {
			PrintWriter out = res.getWriter();

			out.write(JSON.toJSONString(obj, features));
			out.flush();
			out.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void responseJSON(Object obj, SerializerFeature[] features, String contentType) {
		HttpServletResponse res = ContextHolderUtils.getResponse();
		
		res.setContentType(contentType+";charset=utf-8");
		res.setCharacterEncoding("utf-8");
		
		try {
			PrintWriter out = res.getWriter();
			
			out.write(JSON.toJSONString(obj, features));
			out.flush();
			out.close();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 默认对response输出JSON方法(text/html), 所应用的序列化配置如下:
	 * 		枚举类型输出为字符串
	 * 		日期类型转为字符串
	 * @param obj
	 */
	public static void responseJSON(Object obj) {
		SerializerFeature[] features = {
			SerializerFeature.WriteEnumUsingToString,
			SerializerFeature.WriteDateUseDateFormat
		};
		responseJSON(obj, features);
	}
	/**
	 * 默认对response输出JSON方法(按照ContentType输出), 所应用的序列化配置如下:
	 * 		枚举类型输出为字符串
	 * 		日期类型转为字符串
	 * @param obj
	 */
	public static void responseJSON(Object obj, String contentType) {
		SerializerFeature[] features = {
				SerializerFeature.WriteEnumUsingToString,
				SerializerFeature.WriteDateUseDateFormat
		};
		responseJSON(obj, features, contentType);
	}
	
	
	/**
	 * 把字符串直接输出到页面
	 * @param str 字符串
	 */
	public static void responseString(String str) {
		HttpServletResponse res = ContextHolderUtils.getResponse();

		res.setContentType("text/html;charset=utf-8");
		res.setCharacterEncoding("utf-8");
		
		try {
			PrintWriter out = res.getWriter();
			
			out.write(str);
			out.flush();
			out.close();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	
	@SuppressWarnings("rawtypes")
	public static List<Map> parseList(String jsonStr) {
		return JSONArray.parseArray(jsonStr, Map.class);
	}
	
	public static List<?> parseList(String jsonStr,Class<?> clazz) {
		return JSONArray.parseArray(jsonStr, clazz);
	}
	
	@SuppressWarnings("rawtypes")
	public static Map parseMap(String jsonStr) {
		return JSONArray.parseObject(jsonStr, Map.class);
	}
	
	public static String serializer(Object obj) {
		if(obj == null) {
			return "{}";
		}
		
		SerializerFeature[] features = {
				SerializerFeature.WriteEnumUsingToString,
				SerializerFeature.WriteDateUseDateFormat,
				SerializerFeature.DisableCircularReferenceDetect	// 禁止引用
		};
		return JSON.toJSONString(obj, features);
	}
	
//	public static void main(String[] args) {
//		HbglEnvTaskRecord h = new HbglEnvTaskRecord();
//		h.setAdd_ip("s");
//		h.setArea("fas");
//		h.setLast_edit_by("asf");
//		String x = "[{\"add_ip\":\"s\",\"area\":\"fas\",\"last_edit_by\":\"asf\"}]";
//		System.out.println(serializer(h));
//		System.out.println(parseList("["+serializer(h)+"]",HbglEnvTaskRecord.class));
//	}
}